package com.cloud.point.designPattern.chainResponsibility;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

/**
 * ValidatorManager
 * 检验器管理器
 *
 * @author H
 * @version 1.0
 * @date 2024-03-28 10:11
 */
@Service
public class ValidatorManager implements InitializingBean {

    @Resource
    private ApplicationContext applicationContext;

    private List<ParamValidator> validatorList = new ArrayList<>();


    @Override
    public void afterPropertiesSet() {
        //获取所有校验器
        Map<String, ParamValidator> paramValidatorMap = applicationContext.getBeansOfType(ParamValidator.class);
        validatorList = new ArrayList<>(paramValidatorMap.values());
    }

    /*
    //方式二：构造器注入
    private List<ParamValidator> validatorList;

    public ValidatorManager(List<ParamValidator> validatorList) {
        this.validatorList = validatorList;
    }
    */

    /**
     * 暴露参数校验方法；外部校验参数只用单独调用这个方法，不用关心内部校验；校验器会依次按照顺序校验
     *
     * @param request
     */
    public void checkParam(ParamRequest request) {
        validatorList.forEach(t -> t.checkParam(request));
    }

}
